//
//  RegistViewController.m
//  ShouYi
//
//  Created by yurui on 16/2/24.
//  Copyright © 2016年 yurui. All rights reserved.
//

#import "RegistViewController.h"
#import "LoginViewController.h"
@interface RegistViewController ()<UITextFieldDelegate>
@property(nonatomic,strong) UITextField *phoneTF;
@property(nonatomic,strong) UITextField *mmTF;
@property(nonatomic,strong) UITextField *yzmTF;
@property(nonatomic,strong) UIButton *yzmBt;
@property(nonatomic,strong) UIButton *submitBt;
@property(nonatomic,strong) UILabel *tishiLabel;
@property(nonatomic,strong) UIButton *fuwuBt;

@end

@implementation RegistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"注册";
    self.view.backgroundColor = [UIColor whiteColor];
    [self addSubViews];
}
- (void)addSubViews{
    self.phoneTF = [[UITextField  alloc]initWithFrame:CGRectMake(15, 105, kWidth - 30, 35)];
    _phoneTF.borderStyle = UITextBorderStyleRoundedRect;
    _phoneTF.placeholder = @"手机号";
    _phoneTF.font = [UIFont systemFontOfSize:15];
    _phoneTF.keyboardType = UIKeyboardTypeNumberPad;
    _phoneTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    _phoneTF.delegate =self;
    [self.view addSubview:_phoneTF];
    
    self.mmTF = [[UITextField  alloc]initWithFrame:CGRectMake(15, 160, kWidth - 30, 35)];
    _mmTF.borderStyle = UITextBorderStyleRoundedRect;
    _mmTF.placeholder = @"密码";
    _mmTF.font = [UIFont systemFontOfSize:15];
    _mmTF.secureTextEntry = YES;
    _mmTF.delegate = self;
    _mmTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.view addSubview:_mmTF];
    
    self.yzmTF = [[UITextField  alloc]initWithFrame:CGRectMake(15, 215, kWidth / 2 , 35)];
    _yzmTF.borderStyle = UITextBorderStyleRoundedRect;
    _yzmTF.placeholder = @"验证码";
    _yzmTF.font = [UIFont systemFontOfSize:15];
    _yzmTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    _yzmTF.delegate = self;
    [self.view addSubview:_yzmTF];
    
    self.yzmBt = [UIButton buttonWithType:UIButtonTypeCustom];
    _yzmBt.frame = CGRectMake(kWidth / 2 + 30, 215, kWidth / 2 - 45, 35);
    _yzmBt.backgroundColor = [UIColor redColor];
    [_yzmBt setTitle:@"发送验证码" forState:UIControlStateNormal];
    _yzmBt.titleLabel.font = [UIFont systemFontOfSize:14];
    [_yzmBt addTarget:self action:@selector(getYZMAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_yzmBt];
    
    self.submitBt = [UIButton buttonWithType:UIButtonTypeCustom];
    _submitBt.frame = CGRectMake(15, 280, kWidth - 30, 30);
    _submitBt.backgroundColor = kBtColor;
    [_submitBt setTitle:@"提交" forState:UIControlStateNormal];
    _submitBt.titleLabel.font = [UIFont systemFontOfSize:14];
    [_submitBt addTarget:self action:@selector(tiJiaoAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_submitBt];
    
    self.tishiLabel = [[UILabel alloc]initWithFrame:CGRectMake(45, 320, 140, 15)];
    _tishiLabel.text = @"点击提交，表示已经同意";
    _tishiLabel.textAlignment = NSTextAlignmentRight;
    _tishiLabel.font = [UIFont systemFontOfSize:11];
    _tishiLabel.textColor = [UIColor grayColor];
    [self.view addSubview:_tishiLabel];
    
    self.fuwuBt = [UIButton buttonWithType:UIButtonTypeCustom];
    _fuwuBt.frame = CGRectMake(185, 320, 70, 15);
    [_fuwuBt setTitleColor:kBtColor forState:UIControlStateNormal];
    _fuwuBt.titleLabel.font = [UIFont systemFontOfSize:11];
    [_fuwuBt addTarget:self action:@selector(fuWuTiaoKuan) forControlEvents:UIControlEventTouchUpInside];
    [_fuwuBt setTitle:@"《服务协议》" forState:UIControlStateNormal];
    [self.view addSubview:_fuwuBt];

}
// 获取验证码
- (void)getYZMAction{
    if (_phoneTF.text.length == 0) {
        UIAlertView *aalertV = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请输入手机号" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [aalertV show];
    }else{
    __block int timeout = 60; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout<=0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [_yzmBt setTitle:@"获取验证码" forState:UIControlStateNormal];
                _yzmBt.userInteractionEnabled = YES;
            });
        }else{
            int seconds = timeout % 60;
            NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                //NSLog(@"____%@",strTime);
                [UIView beginAnimations:nil context:nil];
                [UIView setAnimationDuration:1];
                NSString *str = [NSString stringWithFormat:@"重新发送(%@)",strTime];
                [_yzmBt setTitle:str forState:UIControlStateNormal];
                [UIView commitAnimations];
                _yzmBt.userInteractionEnabled = NO;
            });
            timeout--;
        }
    });
    dispatch_resume(_timer);
        [kUserDefault setObject:_phoneTF.text forKey:@"phoneNum"];
        NSString *str = [kUserDefault objectForKey:@"phoneNum"];
        NSDictionary *parmeters = @{@"mobile":str};
        [LORequestManger POST:@"http://wx1.01zg.com/plugins/app/chaozan/main.php?action=yanzm&sid=2216&mobile=" params:parmeters success:^(id response) {
            NSLog(@"response = %@",response);
            NSLog(@"str = %@",str);
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"error = %@",error);
        }];
      }
}
// 提交注册按钮响应
- (void)tiJiaoAction{
    NSLog(@"self.phoneTF***** = %@",self.phoneTF.text);
    if (self.phoneTF.text.length < 11 || self.phoneTF.text.length > 11  ) {
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"手机输入有误" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
    }else{
        
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        [kUserDefault setObject:_phoneTF.text forKey:@"phoneNum"];
        NSString * uuid = [kUserDefault objectForKey:@"CFUUID"];
        NSLog(@"uuid = ----- %@",uuid);
        NSLog(@"_______str = %@",_phoneTF.text);
        NSDictionary *parmeters = @{@"mobile":_phoneTF.text,@"uuid":@"123456",@"yzm":_yzmTF.text,@"userpass":_mmTF.text};
        [manager POST:@"http://wx1.01zg.com/plugins/app/chaozan/main.php?action=userreg&sid=2216&mobile=&userpass=&uuid=&yzm=" parameters:parmeters success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSLog(@"Success:%@",responseObject);
#warning 根据返回值判断手机有没有注册过
            NSLog(@"_______str = %@",_phoneTF.text);
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"erros: %@",error);
        }];
        [self.navigationController popViewControllerAnimated:YES];
    }
}
// 服务条款
- (void)fuWuTiaoKuan{
    NSLog(@"\a");
}
#pragma  mark UITextFieldDelegate---- 响应方法

- ( BOOL )textFieldShouldReturn:( UITextField *)textField{
    [_phoneTF resignFirstResponder];
    [_mmTF resignFirstResponder];
    [self.yzmTF resignFirstResponder];
    return  YES;
}
//点击屏幕空白处去掉键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.phoneTF resignFirstResponder];
    [self.mmTF resignFirstResponder];
    [self.yzmTF resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
